package com.txzt.gaea.provide.utils;

import com.txzt.gaea.api.client.OperationLogRemoteService;
import com.txzt.gaea.common.core.base.context.AdminUserContext;
import com.txzt.gaea.dto.OperationLogDTO;
import com.txzt.gaea.vo.AdminUserVO;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.util.Date;


@Service
public class LogUtils {

    @Value("${logs.open}")
    private String logsOpen;

    private static String logsOpenS;

    @PostConstruct
    public void init(){
        logsOpenS = logsOpen;
    }



    public static void logSave(String type, String typename, String classname) {

        OperationLogRemoteService operationLogRemoteService = (OperationLogRemoteService) SpringContextUtil.getBean(OperationLogRemoteService.class);
        if (StringUtils.equals(logsOpenS, "0")){
            try {
                AdminUserVO user = AdminUserContext.getUser();
                OperationLogDTO logdto = new OperationLogDTO();
                logdto.setType(type);
                logdto.setTypeName(typename);
                if (user != null) {
                    StringBuilder builder = new StringBuilder();
                    builder.append("用户");
                    builder.append(user.getName());
                    builder.append("在");
                    builder.append(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
                    builder.append(typename);
                    logdto.setName(builder.toString());
                    logdto.setUserId(user.getId());
                }
                logdto.setClassName(classname);
                //设置ip
                if (!(Iputil.getIpaddr()==null||"".equals(Iputil.getIpaddr()))){
                    logdto.setClientIp(Iputil.getIpaddr());
                }
                logdto.setToolCode("file-manage");
                logdto.setToolVersion("v1.0");
                operationLogRemoteService.insertLogAsync(logdto);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
}

